package jmine.tec.security.impl.web.pages.grupousuario;

import java.util.ArrayList;
import java.util.List;

import jmine.tec.web.pages.HelpTextMessages;
import jmine.tec.web.wicket.component.button.ActionSubmitLink;
import jmine.tec.web.wicket.pages.Template;

import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;

import bancosys.tec.exception.MessageCreator;
import bancosys.tec.security.annotation.Secure;
import bancosys.tec.security.authorization.UrlPermission;
import bancosys.tec.security.impl.domain.Group;

/**
 * Tela de cadastro de {@link Group}
 * 
 * @author diego.ferreira
 * @created Jan 10, 2011
 */
@Secure(id = "URL_EDIT_GRUPO_USUARIO", permissionType = UrlPermission.class)
public class CadastroGroup extends Template {

    private Group group;

    private Form<Group> form;

    /**
     * Construtor
     * 
     * @param group {@link Group}
     */
    public CadastroGroup(Group group) {
        this.group = group;
        this.initForm();
        this.generateComponents();
    }

    /**
     * Inicializa o formulario.
     */
    private void initForm() {
        this.form = new Form<Group>("mainForm", new CompoundPropertyModel<Group>(this.group));

    }

    /**
     * Gera os componentes utilizados em tela.
     */
    private void generateComponents() {
        this.form.add(new TextField<String>("name"));
        List<ITab> tabs = new ArrayList<ITab>();
        tabs.add(new AbstractTab(new Model<String>("Credential")) {

            @Override
            public Panel getPanel(String panelId) {
                return new CadastroGroupCredential(panelId, CadastroGroup.this.group);
            }
        });
        tabs.add(new AbstractTab(new Model<String>("Chinese Wall")) {

            @Override
            public Panel getPanel(String panelId) {
                return new CadastroGroupChineseWall(panelId, CadastroGroup.this.group);
            }
        });

        TabbedPanel pan = new TabbedPanel("tabs", tabs) {
            @Override
            protected WebMarkupContainer newLink(String linkId, final int index) {
                return new ActionSubmitLink(linkId) {
                    @Override
                    public void onAction() {
                        setSelectedTab(index);
                    }
                };
            }
        };
        this.form.add(pan);
        this.form.add(new ActionSubmitLink("gravar") {
            @Override
            public void onAction() {
                CadastroGroup.this.submitForm();
            }
        });
        this.form.add(new Link("voltar") {

            @Override
            public void onClick() {
                this.setResponsePage(PesquisaGroup.class);
            }
        });
        this.add(this.form);
    }

    /**
     * Chamado no submit do form.
     */
    protected void submitForm() {
        this.group.getPersister().save();
        this.setResponsePage(PesquisaGroup.class);
        this.info("Operação realizada com sucesso.");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected MessageCreator getHelpTextCreator() {
        return HelpTextMessages.GROUP_CADASTRO;
    }
}
